package com.example.rk.service.impl;
import com.example.rk.common.LoggerSearchInfo;
import com.example.rk.common.MyPageInfo;
import com.example.rk.entity.Changest;
import com.example.rk.entity.Company;
import com.example.rk.mapper.CompanyMapper;
import com.example.rk.service.CompanyService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Service("CompanyService")
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyMapper mapper;

    @Override
    public void deleteById(int id) {
        mapper.deleteById(id);
    }

    @Override
    public String doAdd(Company company) {
        Company dbcompany=mapper.findadd(company.getName(),company.getPosition());
        if (dbcompany==null){
            mapper.create(company);
            return "ok";
        }else{
            return "exist";
        }

    }

    @Override
    public Company findById(int id) {
        return mapper.findByID(id);
    }

    @Override
    public String doEdit(Company company) {
        try {
            mapper.doEdit(company);
            return "ok";
        }catch (Exception e){
            System.out.println("CompanyServiceImpl.doEdit() ERROR:e="+e);
        }
        return "error";

    }

    @Override
    public List<Company> findonly(int id) {
        return mapper.findonly(id);
    }

    @Override
    public MyPageInfo findBySearch1(HttpServletRequest request, int id) {
        int currentPageNum=1;  //当前页
        int pageSize=5;  //每页大小
        String currentPageNumStr = request.getParameter("pageNum");
        if (currentPageNumStr!=null){
            currentPageNum=Integer.parseInt(currentPageNumStr);
        }
        String name = request.getParameter("name");
        String position = request.getParameter("position");
        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在执行sql之前执行
        mapper.findBySearch1(id,name,position);
        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(),pageInfo.getPageSize(),
                pageInfo.getNavigateLastPage(),pageInfo.getTotal(),pageInfo.getList());
        myPageInfo.setSearch1(name);
        myPageInfo.setSearch2(position);
        myPageInfo.setSearch3(id);

        return myPageInfo;

    }

    @Override
    public void setImageUrl(String name, int id) {
        mapper.setImageUrl( name,id);
    }

    @Override
    public String creattupian(Changest changest)
    {
        try {
            mapper.creattupian(changest);
            return "ok";
        }catch (Exception e){return "error";}
    }

    @Override
    public MyPageInfo findBySearch(HttpServletRequest request) {
        int currentPageNum=1;  //当前页
        int pageSize=5;  //每页大小
        String currentPageNumStr = request.getParameter("pageNum");
        if (currentPageNumStr!=null){
            currentPageNum=Integer.parseInt(currentPageNumStr);
        }
        String name = request.getParameter("name");
        String position = request.getParameter("position");
        LoggerSearchInfo.loggerAdminSearchInfo1("location",name);
        LoggerSearchInfo.loggerAdminSearchInfo("position",position);
        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在执行sql之前执行
        mapper.findBySearch(name,position);
        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(),pageInfo.getPageSize(),
                pageInfo.getNavigateLastPage(),pageInfo.getTotal(),pageInfo.getList());
        myPageInfo.setSearch1(name);
        myPageInfo.setSearch2(position);

        return myPageInfo;

    }

}


